﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Threading.Tasks;

namespace Von.Web.Framework
{
    #region "Resources"
    /// <summary>
    /// 资源文件读取类
    /// </summary>
    public class ResourceFileReader
    {
        public string Filename { get; private set; }
        System.Resources.ResourceReader reader;
        public ResourceFileReader(string fileName)
        {
            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\wwwroot\\Resource\\", fileName + ".resources", SearchOption.AllDirectories);
            if (files.Length > 0)
            {
                reader = new ResourceReader(files[0]);
                Filename = fileName;
            }
            else Filename = "";
        }
        public ResourceFileReader(Type t)
        {
            reader = new ResourceReader(t.Assembly.Location);
            if (reader == null) Filename = ""; else Filename = t.ToString();
        }
        public string this[int Index]
        {
            get
            {
                IDictionaryEnumerator dicts = reader.GetEnumerator();
                int i = 0;
                while (dicts.MoveNext() && i < Index) i++;
                if (i == Index) return dicts.Value.ToString();
                else return "";
            }
        }
        public string this[string Name]
        {
            get
            {
                IDictionaryEnumerator dicts = reader.GetEnumerator();
                int i = 0;
                while (dicts.MoveNext())
                {
                    if (dicts.Key.ToString().Equals(Name, StringComparison.OrdinalIgnoreCase))
                        return dicts.Value.ToString();
                }
                return "";
            }
        }
        public string GetValue(string Name, params object[] args)
        {
            return string.Format(this[Name], args);
        }
    }
    /// <summary>
    /// Extension methods for setting up localization services in an Microsoft.Extensions.DependencyInjection.IServiceCollection.
    /// </summary>
    public static class LocalizationServiceCollectionExtensions
    {
        //
        // 摘要:
        //     Adds services required for application localization.
        //
        // 参数:
        //   services:
        //     The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the services
        //     to.
        //
        // 返回结果:
        //     The Microsoft.Extensions.DependencyInjection.IServiceCollection so that additional
        //     calls can be chained.
        public static IServiceCollection AddPortal(this IServiceCollection services)
        {
            //ServiceDescriptor item = new ServiceDescriptor(typeof(Von.Portal.Controllers.AccountController), new Von.Portal.Controllers.AccountController());
            //services.Add(item);
            return services;
        }
    }
    public class Resource
    {
        /// <summary>
        /// 按照文件名获取资源信息内容
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <param name="code">编码</param>
        /// <param name="args">获取参数</param>
        /// <returns></returns>
        public static string GetResourceValue(string filename, string code, params object[] args)
        {
            string result = "";
            ResourceFileReader r = Runtime.GetResourceFileReader(filename);
            if (r != null) result = r.GetValue(code, args);
            if (r == null || string.IsNullOrEmpty(result)) r = Runtime.GetResourceFileReader("VonPortal");
            if (r != null) result = r.GetValue(code, args);
            if (r == null || string.IsNullOrEmpty(result)) r = Runtime.GetResourceFileReader("WebApp");
            if (r != null) result = r.GetValue(code, args);
            if (r == null || string.IsNullOrEmpty(result))
            {
                string fmt = "[" + code + "]";
                for (int i = 0; i < args.Length; i++) fmt += "{" + (i).ToString() + "}";
                result = string.Format(fmt, args);
            }
            return result;
        }
    }
    #endregion
}
